package com.example.weibo_app.controller;

import com.example.weibo_app.common.utils.AjaxResult;
import com.example.weibo_app.domain.User;
import com.example.weibo_app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    UserService service;

    @GetMapping("/getUserListAll")
    @ApiOperation(value = "获取用户列表(用户id和用户名)")
    public AjaxResult getUserListAll() {
        return AjaxResult.success(service.selectAll());
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户")
    public AjaxResult addUser(@Valid @RequestBody User user) {
        return AjaxResult.success(service.SaveUser(user));
    }

    @PostMapping("/UpdateUser")
    @ApiOperation(value = "修改用户密码")
    public AjaxResult UpdateUser(@Valid @RequestBody User user) {
        System.out.println(service.UpdateUser(user));
        if(service.UpdateUser(user) != 1){
            return AjaxResult.error("id错误");
        }
        return AjaxResult.success("1");
    }

    @PostMapping("/DeleteUser")
    @ApiOperation(value = "删除用户")
    public AjaxResult DeleteUser(int id) {
        return AjaxResult.success(service.DeleteUser(id));
    }

    @PostMapping("/UserLogin")
    @ApiOperation(value = "用户登录")
    public AjaxResult UserLogin(@Valid @RequestBody User user) {
        return AjaxResult.success(service.checkUser(user));
    }
}
